package project;

	import java.io.BufferedReader;
	import java.io.File;
	import java.io.FileInputStream;
	import java.io.FileReader;
	import java.io.FileWriter;
	import java.io.IOException;
	import java.io.InputStreamReader;
	import java.io.PrintWriter;
	import java.util.ArrayList;
	import java.util.HashMap;
	import java.util.Iterator;
	import java.util.Set;

	public class User_To_Item {
		 HashMap<String, ArrayList<String>> user_to_item = new HashMap<String, ArrayList<String>>();
			public void readUser_To_Item() throws IOException
			{ 

				String file_path="D:\\sk\\softwareproject\\homework\\Team\\recommender_system_data\\ratings_train_test.txt";
				File file;
				FileInputStream fr;
				InputStreamReader br;

				file = new File(file_path);
				if (!file.exists()){
					System.out.println("\""+file_path+"\" does not exsit!");
					return;
				}
				
				fr = new FileInputStream(file);
				br = new InputStreamReader (fr);
				BufferedReader BufferedReader = new BufferedReader(br);
				String lineText;
				while((lineText = BufferedReader.readLine()) != null){
					String userid,movieid,rate;
					String[] data = lineText.split("::"); 
					userid = data[0];
					movieid = data[1];
					rate = data[2];
					if(!user_to_item.containsKey(data[0])){
						ArrayList<String> arr = new ArrayList<String>();
						arr.add(movieid+ ":" + rate);
						user_to_item.put(userid, arr);
						
					}
					else{
						user_to_item.get(userid).add(movieid+ ":" + rate);
						
					}
				}
				
				br.close();
				

				FileWriter fw;
				PrintWriter pw;
				Set set = user_to_item.keySet();
				Iterator<String> it = set.iterator();
				file = new File("D:\\sk\\softwareproject\\homework\\Team\\recommender_system_data\\user_to_item.txt");
				if (!file.exists()){
					file.createNewFile();
				}
			    fw = new FileWriter(file);
			    pw = new PrintWriter(fw);
				while(it.hasNext()){
				    String key = it.next();
				    ArrayList<String> arr2 = user_to_item.get(key);
				    for(int i = 0; i < arr2.size() ;i ++){
						pw.println(arr2.get(i));
					}
				}
				pw.close();
			}
			
			
			public ArrayList<String> getdata(String movieid){
				return user_to_item.get(movieid);
			}
			
	}


